package com.irdstudio.apicenter.console.controller.authenticate;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppwxRespDetail;
import com.irdstudio.apicenter.console.service.authenticate.AppwxRespDetailService;


@Controller
@RequestMapping(value = "/appwxRespDetail")
public class AppwxRespDetailAction extends MacmsBaseAction {
	@Autowired
	private AppwxRespDetailService appwxRespDetailService;

	// 插入跳转
	@RequestMapping(value = "jumpAddwxRespDetail")
	public String jumpAddwxRespDetail(HttpServletRequest request) {
	    String appid = request.getParameter("appid");
		request.setAttribute("appid", appid);
		return "auth/addwxRespDetail";
	}

	// 插入保存
	@RequestMapping(value = "addwxRespDetail")
	public void addwxRespDetail(@ModelAttribute("appwxRespDetail") AppwxRespDetail appwxRespDetail,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		//将转义字符转回&
		appwxRespDetail.setWx_resp_pic_path(appwxRespDetail.getWx_resp_pic_path().replaceAll("~", "&"));
		appwxRespDetail.setWx_resp_pic_url(appwxRespDetail.getWx_resp_pic_url().replaceAll("~", "&"));
		flag = appwxRespDetailService.insertOne(appwxRespDetail);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 更新跳转
	@RequestMapping(value = "jumpUpdatewxRespDetail")
	public String jumpUpdatewxRespDetail(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		
		//String wx_resp_actionid, int wx_resp_rank
		
		String recordKey = request.getParameter("recordKey");
		int wx_resp_rank = Integer.parseInt(request.getParameter("wxRespRank"));
		String appid = request.getParameter("appid");
		AppwxRespDetail appwxRespDetail = appwxRespDetailService.queryAppTradePrivate(recordKey,wx_resp_rank);
		request.setAttribute("appwxRespDetail", appwxRespDetail);
		request.setAttribute("appid", appid);
		return "auth/updatewxRespDetail";
	}

	// 更新保存
	@RequestMapping(value = "updatewxRespDetail")
	public void updatewxRespDetail(@ModelAttribute("appwxRespDetail") AppwxRespDetail appwxRespDetail,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		//将转义字符转回&
		appwxRespDetail.setWx_resp_pic_path(appwxRespDetail.getWx_resp_pic_path().replaceAll("~", "&"));
		appwxRespDetail.setWx_resp_pic_url(appwxRespDetail.getWx_resp_pic_url().replaceAll("~", "&"));
		flag = appwxRespDetailService.update(appwxRespDetail);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 删除
	@RequestMapping(value = "deletewxRespDetail")
	public void deletewxRespDetail(HttpServletRequest request,
			HttpServletResponse response) throws SQLException,
			ClassNotFoundException {
		int flag = -1;
		String recordKey = request.getParameter("recordKey");
		int wx_resp_rank = Integer.parseInt(request.getParameter("wxRespRank"));
		flag = appwxRespDetailService.delete(recordKey, wx_resp_rank);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 查看
	@RequestMapping(value = "querywxRespDetailDetail")
	public String querywxRespDetailDetail(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String recordKey = request.getParameter("recordKey");
		int wx_resp_rank = Integer.parseInt(request.getParameter("wxRespRank"));
		String appid = request.getParameter("appid");
		AppwxRespDetail appwxRespDetail = appwxRespDetailService.queryAppTradePrivate(recordKey, wx_resp_rank);
		request.setAttribute("appwxRespDetail", appwxRespDetail);
		request.setAttribute("appid", appid);
		return "auth/querywxRespDetailDetail";
	}

}

